package df.bean.obj.doctor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;
import df.bean.db.table.Doctor;
import df.bean.db.table.OrderItem;
import df.bean.db.table.PaymentMode;
import df.bean.db.table.SummaryDaily;
import df.bean.db.table.SummaryMonthly;
import df.bean.db.table.SummaryTax406;
import df.bean.db.table.TRN_Error;
import df.bean.db.table.TrnDaily;
import df.bean.obj.Item.DrMethodAllocation;
import df.bean.obj.invoice.InvoiceDetail;
import df.bean.obj.util.DialogBox;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Utils;
import df.bean.obj.util.Variables;
import df.bean.obj.variable.SummaryMonthlyVar;


public class CareProvider_280410 {
    private Doctor doctor = null;
    private ResultSet rs = null;
    private Statement statement = null;
    // use for compute daily
    private TrnDaily trnDaily = null;
    private DrMethodAllocation drMethodAllocation = null;
    
    // use for compute monthly
//    private SummaryMonthly summaryMonthly = null;
    
    InvoiceDetail invoiceDetail = null;
    DBConnection conn = null;
    
    public static SummaryMonthlyVar smVar = null;

    private String doctorCode;
    private String className;
    private String hospitalCode;
    private String invoiceNo;
    private String treatmentType;
    private String lineNo;
    private String receiptDate;
    
    public String lYyyy, lMm, lDd, lHospitalCode, lInvNo, lInvDate, lHno, lPatientName, lDoctorDepartmentCode;
    public String lLocationCode, lLineNo, lOrderItemCode, lReceiptModeCode, lReceiptTypeCode, lPaymentModule, lDoctorCode;
    public Double lRecAmt, lInvAmt, lInvDiscountAmt, lTotPremiumAmt, lNorAllocateAmt, lNorAllocatePct, lDrAmt, lDrPremium;
    public Double lDrTax400, lDrTax401, lDrTax402, lDrTax406, lHpAmt, lHpPremium, lHpTax, lPremiumChargePct, lPremiumRecAmt;
    public String lStatusCode, lTaxTypeCode, lAdmissionTypeCode, lDoctorTreatmentCode, lTransactionDate, lOrderDate;
    public String lDoctorCategoryCode, lExcludeTreatment;
    public String lInvIsVoid, lRecIsVoid,lPrivateDoctor;
    public Double lNorAllocateMinAmt, lNorAllocateMaxAmt;
    public Double lguaranteeAmt;
    static private SummaryDaily sd;
    public String message = "";
    public String guaranteeMsg = "";
    
    public CareProvider_280410() {
    }
    
    public CareProvider_280410(DBConnection conn) {
        this.setDBConnection(conn);
//        sd = new SummaryDaily(this.getDBConnection());
    }
    public DBConnection getDBConnection() {
        return this.conn;
    }
    public void setDBConnection(DBConnection conn) {
        this.conn = conn;
    }
    
//    @Override
//    protected void finalize() {
//        doctor = null;
//        rs = null;
//        setDrMethodAllocation(null);
//        invoiceDetail = null;
//    }
    
    public CareProvider_280410(String doctorCode, String hospitalCode, DBConnection conn) {
        this.setDoctorCode(doctorCode);
        this.setHospitalCode(hospitalCode);
        
        this.setDBConnection(conn);
        this.newDoctor();
    }

    // new Doctor
    public void newDoctor() {
        //doctor = new Doctor(this.getDoctorCode(), this.getHospitalCode(), this.getDBConnection());      
        doctor = new Doctor();
    }
    
    public Doctor getDoctor() {
        return this.doctor;
    }
    
    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public String getDoctorCode() {
        return doctorCode;
    }

    public void setDoctorCode(String doctorCode) {
        this.doctorCode = doctorCode;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getHospitalCode() {
        return hospitalCode;
    }

    public void setHospitalCode(String hospitalCode) {
        this.hospitalCode = hospitalCode;
    }

    public String getInvoiceNo() {
        return invoiceNo;
    }

    public void setInvoiceNo(String invoiceNo) {
        this.invoiceNo = invoiceNo;
    }

    public String getTreatmentType() {
        return treatmentType;
    }

    public void setTreatmentType(String treatmentType) {
        this.treatmentType = treatmentType;
    }

    public String getlineNo() {
        return lineNo;
    }

    public void setlineNo(String lineNo) {
        this.lineNo = lineNo;
    }
    
    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    public Statement getStatement() {
        return statement;
    }
    
    public void setTrnDaily(TrnDaily trnDaily) {
        this.trnDaily = trnDaily;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

//    public SummaryMonthly getSummaryMonthly() {
//        return summaryMonthly;
//    }
//
//    public void setSummaryMonthly(SummaryMonthly summaryMonthly) {
//        this.summaryMonthly = summaryMonthly;
//    }

    // คำนวณหาค่า Method Allocation
    protected boolean computeMethodAllocation() {
        try {
            double amountAftDiscount = lInvAmt;
            double normalAllocatePct = getDrMethodAllocation().getMethodAllocation().getNormalAllocatePct();
            double normalAllocateAmt = getDrMethodAllocation().getMethodAllocation().getNormalAllocateAmt();
//            double normalAllocatePrice = getDrMethodAllocation().getMethodAllocation().getPrice();
            double drAmount=0, hpAmount=0, amtAftDisc = amountAftDiscount;
            
            // allocate percent
            if (normalAllocatePct != 0) { drAmount = amtAftDisc * normalAllocatePct /100; }  // percent

            // allocate amount
//            if ((normalAllocatePrice == 0) && (normalAllocateAmt != 0)) {
            if (normalAllocateAmt != 0) {	// แก้ไขให้ได้เท่ากับจำนวนเงินที่ fix
                drAmount = amountAftDiscount;
                // ตรวจสอบว่า ถ้าจำนวนเงินมากกว่าที่ setup ไว้  ให้จ่ายได้สูงสุดเท่ากับ ที่ setup ไว้
                if (amountAftDiscount > normalAllocateAmt) { drAmount = normalAllocateAmt; }   
            }

            // allocate fix price	// เอาออก เพราะในการตรวจสอบราคา จะไม่เข้าเงื่อนไขนี้อีก 
//            if ((normalAllocatePrice != 0) && (normalAllocatePrice == amountAftDiscount)) {
//            	drAmount = normalAllocateAmt;
//            }

            // กรณี order item ไม่นำไปคำนวณค่าแพทย์ 
            if ( !trnDaily.getIsCompute().equalsIgnoreCase("Y") ) { normalAllocateAmt = 0; normalAllocatePct=0; drAmount = 0;}
                        
            hpAmount = amountAftDiscount - drAmount;
            
            // set range price min-max
            this.lNorAllocateMinAmt = getDrMethodAllocation().getMethodAllocation().getPrice();
            this.lNorAllocateMaxAmt = getDrMethodAllocation().getMethodAllocation().getPriceMax();
            
            this.lNorAllocateAmt = normalAllocateAmt;
            this.lNorAllocatePct = normalAllocatePct;
            this.lDrAmt = drAmount;
            this.lHpAmt = hpAmount;

            if (getDrMethodAllocation().getMethodAllocation().getGuaranteeSource().equalsIgnoreCase("DF")) {
                this.lguaranteeAmt = this.lDrAmt;
                this.guaranteeMsg = "ALLOC";
            } else if(getDrMethodAllocation().getMethodAllocation().getGuaranteeSource().equalsIgnoreCase("FULL")) {
                this.lguaranteeAmt = amountAftDiscount;
                this.guaranteeMsg = "ALLOC";
            } else{
                this.guaranteeMsg = "";
                this.lguaranteeAmt = 0.00;
            }

        } catch (Exception e) {
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                    TRN_Error.PROCESS_DAILY, "Compute Medthod Allocation is error", 
                    e.getMessage(), "",TRN_Error.ERROR_TYPE_COMPUTE_METHOD_ALLOCATE_ERROR);
            return false;
        }
        return true;
    }
    
    // คำนวณหาค่า Tax
    private boolean computeTax() {
        try {
        // วิธีคำนวณแบบมีการแบ่งหัตถการ (แบบที่ 1)
        this.lDrTax400 = 0d;
        this.lDrTax401 = 0d;
        this.lDrTax402 = 0d;
        this.lDrTax406 = 0d;
        this.lHpTax = 0d;
        double taxAmt = 0d;
//        OrderItem oi = new OrderItem(this.lOrderItemCode, this.hospitalCode, this.getDBConnection());
//        if (oi.getCode() == null) {
        if ((this.trnDaily.getOrderItemCode() == null) || (this.trnDaily.getOrderItemCode().equalsIgnoreCase("")))  {
            System.out.print("Error : CareProvider.computetax() : Order Item '"+ this.lOrderItemCode + "' is not found");
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), TRN_Error.PROCESS_DAILY, 
                                "Error : CareProvider.computetax() : Order Item '"+ this.lOrderItemCode + "' is not found", "", 
                                TRN_Error.ERROR_TYPE_NO_ORDER_ITEM);
//            DialogBox.ShowError("Order Item Code : " + this.lOrderItemCode + " is not found. ");
            return false;
        }
        
        if ((this.trnDaily.getOrderItemIsAllocFullTax() == null) || (this.trnDaily.getOrderItemIsAllocFullTax().equalsIgnoreCase(""))) { this.trnDaily.setOrderItemIsAllocFullTax(OrderItem.ALLOC_FULL_TAX_YES); }
        if (this.trnDaily.getOrderItemIsAllocFullTax().equals(OrderItem.ALLOC_FULL_TAX_NO)) {       // =N : คิดภาษีจากยอดเต็ม
            taxAmt = this.lDrAmt; // กรณีที่ฐานภาษีคิดจากยอดที่แพทย์ได้จริง
            this.lHpTax = this.lHpAmt;
        } else {                            // =Y : 
            taxAmt = this.lInvAmt;//กรณีที่ฐานภาษีคิดจากยอดหักส่วนลดแล้ว + this.lInvDiscountAmt; // กรณีที่ฐานภาษีคิดจากยอดทั้งหมด
            this.lHpTax = 0d;
        }
//        oi = null;
        // ภาษีจะคำนวณจาก ถ้าเป็น หัตถการ ฐานภาษีจะเท่ากับ ส่วนแบ่งที่หมอได้รับ
        // ถ้าไม่ใช่ หัตถการ ฐานภาษีจะเท่ากับ ยอดทั้งหมด หลังจากหักส่วนลดแล้ว  แต่ยังไม่หักค่า premium
        // if get from method allocation
        this.lTaxTypeCode = getDrMethodAllocation().getMethodAllocation().getTaxTypeCode();
        
        if (getDrMethodAllocation().getMethodAllocation().getNormalAllocatePct() > 100 ) {
            taxAmt = this.lDrAmt;
            this.lHpTax = 0d;
        }
        if (!this.trnDaily.getIsCompute().equalsIgnoreCase("Y")) { taxAmt = 0; this.lHpTax = this.lInvAmt; }
        if (this.lDrAmt <= 0) { taxAmt = 0; this.lHpTax = this.lInvAmt; } // เพิ่มเพื่อให้ภาษีเป็น 0 เมื่อแพทย์ไม่ได้รับส่วนแบ่ง
        if (this.lTaxTypeCode.equals("400")) { this.lDrTax400 = taxAmt; }            
        if (this.lTaxTypeCode.equals("401")) { this.lDrTax401 = taxAmt; }
        if (this.lTaxTypeCode.equals("402")) { this.lDrTax402 = taxAmt; }
        if (this.lTaxTypeCode.equals("406")) { this.lDrTax406 = taxAmt; }
        
        } catch (Exception e) {
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                    TRN_Error.PROCESS_DAILY, "Compute Tax is Error", e.getMessage(), "",
                    TRN_Error.ERROR_TYPE_COMPUTE_TAX_ERROR);
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    // คำนวณหาค่า Premium
    protected boolean computePremium() {
        try {
            // Premium 
            double pctOfCharge = 0, pctOfVat = 0, drPremium = 0, hpPremium = 0, totOfPremium = 0;
            
            // if patient was paid by credit card 
            pctOfCharge = this.lPremiumChargePct / 100;
            totOfPremium = (this.lInvAmt * (pctOfCharge + (pctOfCharge * pctOfVat)));
            drPremium = (this.lDrAmt * (pctOfCharge + (pctOfCharge * pctOfVat)));

            this.lDrPremium = Double.parseDouble((new java.text.DecimalFormat("0.00")).format(drPremium));
            this.lTotPremiumAmt = Double.parseDouble((new java.text.DecimalFormat("0.00")).format(totOfPremium));
            hpPremium = this.lTotPremiumAmt - this.lDrPremium;
            this.lHpPremium = Double.parseDouble((new java.text.DecimalFormat("0.00")).format(hpPremium));
            this.lPremiumRecAmt = this.lRecAmt * this.lPremiumChargePct / 100; 
        } catch (Exception e) {
            e.printStackTrace();
            DialogBox.ShowError(this.lInvNo);
            
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                    TRN_Error.PROCESS_DAILY, "Compute Premium is Error", e.getMessage(), "", 
                    TRN_Error.ERROR_TYPE_COMPUTE_PREMIUM_ERROR);
            return false;
        }
        return true;
    }
    
    public String getReceiptDate() {
        return receiptDate;
    }

    public void setReceiptDate(String receiptDate) {
        this.receiptDate = receiptDate;
    }
    
    public boolean computeTax406(String dd, String mm, String yyyy) {
        SummaryTax406 st = new SummaryTax406(this.getDBConnection());
        String mm1 = "01";
        if (mm.equals("12")) { mm1 = "07"; }
        
        try {
               String sql = "select DOCTOR_CODE, sum(SUM_AMT) as sAmt, sum(DR_NET_PAID_AMT) as sDrAmt, sum(HP_SUM_AMT) as sHpAmt,";
               sql = sql + " sum(DR_NET_TAX_406_AMT) as sDrTax, sum(HP_TAX) as sHpTax";
               sql = sql + " from PAYMENT_MONTHLY";
               sql = sql + " where (TAX_406_DATE >= '" + yyyy.concat(mm1.concat("00")) + "'";
               sql = sql + " and TAX_406_DATE <= '" + yyyy.concat(mm.concat(dd)) + "')";
               sql = sql + " and DOCTOR_CODE = '" + this.getDoctorCode() + "'";
               sql = sql + " group by DOCTOR_CODE";

                if (this.getStatement() == null) { this.setStatement(this.getDBConnection().getConnection().createStatement()); }
                rs = this.getStatement().executeQuery(sql);
                
                while (rs.next()) {
//                        values.clear();
                    // call for compute summary monthly
                    

                    // insert into SUMMARY_MONTHLY table
                    st.setHospitalCode(this.getHospitalCode());
                    st.setDoctorCode(this.getDoctorCode());
                    st.setDd(dd);
                    st.setMm(mm);
                    st.setYyyy(yyyy);
                    st.setSumAmt(rs.getDouble("sAmt"));
                    st.setSumDrAmt(rs.getDouble("sDrAmt"));
                    st.setSumHpAmt(rs.getDouble("sHpAmt"));
                    st.setSumTaxDrAmt(rs.getDouble("sDrTax"));
                    st.setSumTaxHpAmt(rs.getDouble("sHpTax"));
                    st.setTextSumDrAmt(Utils.toThaiMoney(rs.getDouble("sDrAmt")));
                    st.setTextSumTaxDrAmt(Utils.toThaiMoney(rs.getDouble("sDrTax")));
                    st.setPrintDate(yyyy+mm+dd);
                    st.setUpdateDate(JDate.getDate());
                    st.setUpdateTime(JDate.getTime());
                    st.setUserId(Variables.getUserID());

                    if (!st.insert()) { st = null;   return false;  }

                }
        } catch (Exception e) {
            e.printStackTrace();
            this.setMessage("Calculate tax 4(6) is error.");
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                    TRN_Error.PROCESS_DAILY, "Calculate tax 4(6) is error.", e.getMessage());
//            DialogBox.ShowError("Error in CareProvider.computeSummaryTax406() \n" + e.getMessage());
            return false;
        } finally {
               //Clean up resources, close the connection.
            st = null;
                try {
                    if(rs != null) {
                        rs.close();
                        rs = null;
                    }
//                    if (stmt != null) {
//                        stmt.close();
//                        stmt = null;
//                    }
                } catch (Exception ignored) { ignored.printStackTrace();   }
            }

        return true;
    }

    public boolean computeTax406(String startDate, String endDate) {
        SummaryTax406 st = new SummaryTax406(this.getDBConnection());
//        String mm1 = "01";
//        if (mm.equals("12")) { mm1 = "07"; }
        String yyyy = endDate.substring(0, 4);
        String mm = endDate.substring(4, 6);
        String dd = endDate.substring(6, 8);
        
        try {
               String sql = "select DOCTOR_CODE, sum(SUM_AMT) as sAmt, sum(DR_NET_PAID_AMT) as sDrAmt, sum(HP_SUM_AMT) as sHpAmt,";
               sql = sql + " sum(DR_NET_TAX_406_AMT) as sDrTax, sum(HP_TAX) as sHpTax";
               sql = sql + " from PAYMENT_MONTHLY";
               sql = sql + " where (TAX_406_DATE >= '" + startDate + "'";
               sql = sql + " and TAX_406_DATE <= '" + endDate + "')";
               sql = sql + " and DOCTOR_CODE = '" + this.getDoctorCode() + "'";
               sql = sql + " group by DOCTOR_CODE";

                if (this.getStatement() == null) { this.setStatement(this.getDBConnection().getConnection().createStatement()); }
                rs = this.getStatement().executeQuery(sql);
                
                while (rs.next()) {
//                        values.clear();
                    // call for compute summary monthly
                    
                    // insert into SUMMARY_MONTHLY table
                    st.setHospitalCode(this.getHospitalCode());
                    st.setDoctorCode(this.getDoctorCode());
                    st.setDd(dd);
                    st.setMm(mm);
                    st.setYyyy(yyyy);
                    st.setSumAmt(rs.getDouble("sAmt"));
                    st.setSumDrAmt(rs.getDouble("sDrAmt"));
                    st.setSumHpAmt(rs.getDouble("sHpAmt"));
                    st.setSumTaxDrAmt(rs.getDouble("sDrTax"));
                    st.setSumTaxHpAmt(rs.getDouble("sHpTax"));
                    st.setTextSumDrAmt(Utils.toThaiMoney(rs.getDouble("sDrAmt")));
                    st.setTextSumTaxDrAmt(Utils.toThaiMoney(rs.getDouble("sDrTax")));
                    st.setPrintDate(yyyy+mm+dd);
                    st.setUpdateDate(JDate.getDate());
                    st.setUpdateTime(JDate.getTime());
                    st.setUserId(Variables.getUserID());

                    if (!st.insert()) { st = null;   return false;  }

                }
        } catch (Exception e) {
            e.printStackTrace();
            this.setMessage("Calculate tax 4(6) is error.");
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                    TRN_Error.PROCESS_DAILY, "Calculate tax 4(6) is error.", e.getMessage());
//            DialogBox.ShowError("Error in CareProvider.computeSummaryTax406() \n" + e.getMessage());
            return false;
        } finally {
               //Clean up resources, close the connection.
            st = null;
                try {
                    if(rs != null) {
                        rs.close();
                        rs = null;
                    }
//                    if (stmt != null) {
//                        stmt.close();
//                        stmt = null;
//                    }
                } catch (Exception ignored) { ignored.printStackTrace();   }
            }

        return true;
    }

    public DrMethodAllocation getDrMethodAllocation() {
        return drMethodAllocation;
    }

    public void setDrMethodAllocation(DrMethodAllocation drMethodAllocation) {
        this.drMethodAllocation = drMethodAllocation;
    }
    
    // Compute all of care provider
    public boolean computeTransDaily() throws SQLException {
        boolean ret = true;

        try {
            if (this.getDrMethodAllocation() == null) {
                TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), TRN_Error.PROCESS_DAILY, "Method Allocation is null", "", "", TRN_Error.ERROR_TYPE_METHOD_ALLOCATION_IS_NULL);
//                DialogBox.ShowError( "Error (ERR005): Method Allocation is null " ); return false;
            }

                // Receipt Detail
                this.lHospitalCode = trnDaily.getHospitalCode();
                this.lInvNo = trnDaily.getInvoiceNo();
                this.lLineNo = trnDaily.getLineNo();
                this.lOrderItemCode = trnDaily.getOrderItemCode();
                this.lDoctorCategoryCode = this.getDoctor().getDoctorCategoryCode();
                this.lDoctorCode = this.getDoctorCode(); 
                this.lInvAmt = trnDaily.getAmountAftDiscount();
                this.lInvDiscountAmt = trnDaily.getAmountOfDiscount();
                this.lAdmissionTypeCode = trnDaily.getAdmissionTypeCode();
                this.lDoctorTreatmentCode = trnDaily.getInvoiceType(); //DoctorTreatment.TYPE_CURE; 
                this.lReceiptTypeCode = trnDaily.getReceiptTypeCode();
                this.lRecAmt = lInvAmt + lInvDiscountAmt;
                this.lTransactionDate = trnDaily.getTransactionDate();
                this.lInvIsVoid = trnDaily.getInvIsVoid();
                this.lRecIsVoid = trnDaily.getRecIsVoid();
                // set values to list
                this.lInvDate = trnDaily.getInvoiceDate();
                this.lHno = trnDaily.getHnNo();
                // ถ้ามี code แพทย์ ถือว่าเป็น "Y"
                // ถ้า private doctor = 'N' , '', null ให้เป็น 'N'
                if (trnDaily.getPrivateDoctor().equalsIgnoreCase("N") || 
                		trnDaily.getPrivateDoctor().equalsIgnoreCase("") || 
                		trnDaily.getPrivateDoctor().equals(null)) 
                { this.lPrivateDoctor = "N"; } else { this.lPrivateDoctor = "Y"; }

//                this.lPatientName = rs.getString("PATIENT_NAME");
//                this.lDepartmentGroupCode = rs.getString("departmentGroupCode");
//                this.lLocationCode = rs.getString("locationCode");

                //receipt
//                this.lBillNo = rs.getString("receiptNo");
//                this.lRecDate = rs.getString("receiptDate"); 
//                this.lReceiptModeCode = rs.getString("receiptModeCode");
//                this.lPaymentModule = rs.getString("receiptModule");

//                this.lYyyy = this.lRecDate.substring(0, 4);
//                this.lMm = this.lRecDate.substring(4, 6);
//                this.lDd = this.lRecDate.substring(6,8);
//                this.lReceiptType = rs.getString("receiptType");

//                if (lReceiptType.equals(Status.RECEIPT_TYPE_WRITEOFF)) {
//                    this.lInvDiscountAmt = this.lRecAmt - (this.lRecAmt * (((rs.getDouble("recAmountBefDiscount")-rs.getDouble("recAmountOfDiscount")) * 100) / rs.getDouble("recAmountBefDiscount")));
//                    this.lInvAmt = this.lRecAmt - this.lInvDiscountAmt; 
//                }

//                Double invAmt = this.lInvAmt;
//                if (this.lInvAmt < 0) { invAmt = this.lInvAmt * -1; }
                // Method Allocation
                // find on table Personal
                this.getDrMethodAllocation().useMethodAllocationPersonal();
                if (getDrMethodAllocation().getMethodAllocation().IsFound(this.lOrderItemCode, this.lDoctorTreatmentCode, this.lDoctorCode, this.lAdmissionTypeCode, this.lInvAmt, this.lPrivateDoctor)) { } else {
//                    if (getDrMethodAllocation().getMethodAllocation().IsFound(this.lOrderItemCode, this.lDoctorTreatmentCode, this.lDoctorCode, this.lAdmissionTypeCode, 0d, this.lPrivateDoctor)) { } else {
                        
                        // find on table item
                        this.getDrMethodAllocation().useMethodAllocationNormal();
                        // check method allocation is found
                        if (getDrMethodAllocation().getMethodAllocation().IsFound(this.lOrderItemCode, this.lDoctorTreatmentCode, this.lDoctorCategoryCode, this.lAdmissionTypeCode, this.lInvAmt, this.lPrivateDoctor)) { } else {
//                            if (getDrMethodAllocation().getMethodAllocation().IsFound(this.lOrderItemCode, this.lDoctorTreatmentCode, this.lDoctorCategoryCode, this.lAdmissionTypeCode, 0d, this.lPrivateDoctor)) { } else {
                                
                                // find on table item category
                                this.getDrMethodAllocation().useMethodAllocItemCategory();
                                if (getDrMethodAllocation().getMethodAllocation().IsFound(trnDaily.getOrderItemCategoryCode(), this.lDoctorTreatmentCode, this.lDoctorCategoryCode, this.lAdmissionTypeCode, this.lInvAmt, this.lPrivateDoctor)) { } else {
//                                    if (getDrMethodAllocation().getMethodAllocation().IsFound(trnDaily.getOrderItemCategoryCode(), this.lDoctorTreatmentCode, this.lDoctorCategoryCode, this.lAdmissionTypeCode, 0d, this.lPrivateDoctor)) { } else {
                                
                                        // find on table Master
                                        this.getDrMethodAllocation().useMethodAllocationMaster();
                                        if (!getDrMethodAllocation().getMethodAllocation().IsFound("", this.lDoctorTreatmentCode, this.lDoctorCategoryCode, this.lAdmissionTypeCode, 0d, this.lPrivateDoctor)) {
                                            ret = false; // by default
                                            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(),
                                                    TRN_Error.PROCESS_DAILY, "Error : Inv No : " + this.lInvNo + " is not found condition : " +
                                                                 " Doctor Code : '" + this.lDoctorCode + "'" +
                                                                 " doctor category code : '" + this.getDoctor().getDoctorCategoryCode() + "'" + 
                                                                 " 0rder Item Category code : '" + trnDaily.getOrderItemCategoryCode() + "'" + 
                                                                 " Order Item : '" + this.lOrderItemCode + "'" + 
                                                                 " Treatment : '" + this.lDoctorTreatmentCode + "'" + 
                                                                 " Private Doctor : '" + this.lPrivateDoctor + "'" + 
                                                                 " Admission Type : '" + this.lAdmissionTypeCode + "'", "", 
                                                                 TRN_Error.ERROR_TYPE_NOT_SETUP_CONDITION);
                                            this.setMessage("Error : Not found condition : " +
                                                                 " Doctor Code : '" + this.lDoctorCode + "'" +
                                                                 " doctor category code : '" + this.getDoctor().getDoctorCategoryCode() + "'" + 
                                                                 " 0rder Item Category code : '" + trnDaily.getOrderItemCategoryCode() + "'" + 
                                                                 " Order Item : '" + this.lOrderItemCode + "'" +
                                                                 " Private Doctor : '" + this.lPrivateDoctor + "'" + 
                                                                 " Admission Type : '" + this.lAdmissionTypeCode + "'");
                                            ////break;
                                        }
//                                    } // ตรวจสอบการคำนวณด้วยราคา 0 (หมายถึงไม่ระบุราคา) (Item Category)
                                }   // ตรวจสอบการคำนวณด้วยราคา (Item Category)
                                
//                            }   // ตรวจสอบการคำนวณด้วยราคา 0 (หมายถึงไม่ระบุราคา)
                        }   // ตรวจสอบการคำนวณด้วยราคา
//                    }   // ตรวจสอบการคำนวณด้วยราคา 0 (หมายถึงไม่ระบุราคา)
                }   // ตรวจสอบการคำนวณด้วยราคา

                // Method Allocation
                if (ret) { 
                	this.computeMethodAllocation();

	                // Tax Base
	                if (!this.computeTax()) {  ret = false; }
	
	                // this.lDoctorCategoryCode = getDrMethodAllocation().getDoctorCategoryCode();
	                this.lExcludeTreatment = getDrMethodAllocation().getMethodAllocation().getExcludeTreatment();
	                this.lPremiumChargePct = trnDaily.getPremiumChargePct();
	                this.computePremium();

		            // set variables and update Status to calculated
		            if (!this.updateTransDaily()) {  
		                TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
		                        TRN_Error.PROCESS_DAILY, "", "Update transaction is error."
		                        ,"", TRN_Error.ERROR_TYPE_UPDATE_TRNASACTION_ERROR);
		                ret = false;  
		            } 
                }
//            else {  if  (!ret) { ret = false; break; } } 
////            }

	        } catch (Exception e) {
	            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), TRN_Error.PROCESS_DAILY," ", 
	                    e.getMessage(), "", TRN_Error.ERROR_TYPE_COMPUTE_DAILY_ERROR);
	            e.printStackTrace(); ret = false;
	        } finally {
                //Clean up resources, close the connection.
//                try {
//                    if(rs != null) { rs.close();  rs = null;   }
//                    if (stmt != null) {  stmt.close();  stmt = null;   }
//                    setDrMethodAllocation(null); 
//                } catch (Exception ignored) { 
//                    ignored.printStackTrace(); 
//                    ret = false;
//                }
        }

        return ret;
    }
    

    private boolean updateTransDaily() {
        boolean ret = false;
        TrnDaily td = this.trnDaily;

        td.setHospitalCode(trnDaily.getHospitalCode());
        td.setInvoiceNo(trnDaily.getInvoiceNo());
        td.setInvoiceDate(trnDaily.getInvoiceDate());
        td.setTransactionDate(trnDaily.getTransactionDate());
        td.setHnNo(trnDaily.getHnNo());
        td.setPatientName(trnDaily.getPatientName());
        td.setEpisodeNo(trnDaily.getEpisodeNo());
        td.setPayorOfficeCode(trnDaily.getPayorOfficeCode());
        td.setPayorOfficeName(trnDaily.getPayorOfficeName());
        td.setTransactionModule(trnDaily.getTransactionModule());
        td.setTransactionType(trnDaily.getTransactionType());
        td.setPayorOfficeCategoryCode(trnDaily.getPayorOfficeCategoryCode());
        td.setPayorOfficeCategoryDescription(trnDaily.getPayorOfficeCategoryDescription());
        td.setIsWriteOff(trnDaily.getIsWriteOff());
        td.setLineNo(trnDaily.getLineNo());
        td.setAdmissionTypeCode(trnDaily.getAdmissionTypeCode());
        td.setNationalityCode(trnDaily.getNationalityCode());
        td.setNationalityDescription(trnDaily.getNationalityDescription());
        td.setPatientDepartmentCode(trnDaily.getPatientDepartmentCode());
        td.setPatientLocationCode(trnDaily.getPatientLocationCode());
        td.setReceiptDepartmentCode(trnDaily.getReceiptDepartmentCode());
        td.setReceiptLocationCode(trnDaily.getReceiptLocationCode());
//        td.setDoctorDepartmentCode(trnDaily.getDoctorDepartmentCode());
        td.setDoctorDepartmentCode(this.getDoctor().getDepartmentCode());
        td.setOrderItemCode(trnDaily.getOrderItemCode());
        td.setOrderItemDescription(trnDaily.getOrderItemDescription());
        td.setDoctorCode(trnDaily.getDoctorCode());
        td.setVerifyDate(trnDaily.getVerifyDate());
        td.setVerifyTime(trnDaily.getVerifyTime());
        td.setDoctorExecuteCode(trnDaily.getDoctorExecuteCode());
        td.setExecuteDate(trnDaily.getExecuteDate());
        td.setExecuteTime(trnDaily.getExecuteTime());
        td.setDoctorResultCode(trnDaily.getDoctorResultCode());
        td.setOldDoctorCode(trnDaily.getDoctorCode());
        td.setReceiptTypeCode(trnDaily.getReceiptTypeCode());
        td.setAmountBefDiscount(trnDaily.getAmountBefDiscount());
        td.setAmountOfDiscount(trnDaily.getAmountOfDiscount());
        td.setAmountAftDiscount(trnDaily.getAmountAftDiscount());
        td.setAmountBefWriteOff(trnDaily.getAmountBefWriteOff());
        td.setInvIsVoid(trnDaily.getInvIsVoid());
        td.setRecIsVoid(trnDaily.getRecIsVoid());
        td.setUpdateDate(JDate.getDate());
        td.setUpdateTime(JDate.getTime());
        td.setUserId(Variables.getUserID());
        td.setInvoiceType(trnDaily.getInvoiceType());
        td.setTotalBillAmount(trnDaily.getTotalBillAmount());
        td.setTotalDrRecAmount(trnDaily.getTotalDrRecAmount());
        td.setOldAmount(trnDaily.getAmountAftDiscount());
        td.setActive(trnDaily.getActive());
        
        td.setTaxTypeCode(this.lTaxTypeCode);
        td.setPremiumRecAmt(this.lTotPremiumAmt);
        td.setNormalAllocateMinAmt(this.lNorAllocateMinAmt);	// set min range
        td.setNormalAllocateMaxAmt(this.lNorAllocateMaxAmt);	// set max range
        td.setNorAllocateAmt(this.lNorAllocateAmt);
        td.setNorAllocatePct(this.lNorAllocatePct);
        td.setDrAmt(this.lDrAmt);
        td.setOldDrAmt(this.lDrAmt);    // keep dr_amt
        td.setDrPremium(this.lDrPremium);
        td.setDrTax400(this.lDrTax400);
        td.setDrTax401(this.lDrTax401);
        td.setDrTax402(this.lDrTax402);
        td.setDrTax406(this.lDrTax406);
        
        td.setHpAmt(this.lHpAmt);
        td.setHpPremium(this.lHpPremium);
        td.setHpTax(this.lHpTax);
        
        td.setPremiumChargePct(this.lPremiumChargePct);
        td.setDoctorCategoryCode(this.lDoctorCategoryCode);
        td.setExcludeTreatment(this.lExcludeTreatment);
        td.setPremiumRecAmt(this.lPremiumRecAmt);
        td.setComputeDailyDate(JDate.getDate());
        td.setComputeDailyTime(JDate.getTime());
        td.setComputeDailyUserID(this.guaranteeMsg+":"+Variables.getUserID());
        
        td.setYyyy(trnDaily.getYyyy());
        td.setMm(trnDaily.getMm());
        td.setReceiptNo(trnDaily.getReceiptNo());
        td.setReceiptDate(trnDaily.getReceiptDate());
        td.setPayByCash(trnDaily.getPayByCash());
        td.setPayByAR(trnDaily.getPayByAR());
        td.setPayByDoctor(trnDaily.getPayByDoctor());
        td.setPayByPayor(trnDaily.getPayByPayor());
        td.setPayByCashAr(trnDaily.getPayByCashAr());
        td.setIsPaid(trnDaily.getIsPaid());
        td.setOrderItemCategoryCode(trnDaily.getOrderItemCategoryCode());
        td.setGuaranteeAmt(this.lguaranteeAmt);

        // insert
        ret = true;
        ret = td.update();
        td = null;
        if (!ret) { DialogBox.ShowError("Error : " + td.getInvoiceNo()); }
        return ret;
    }
//    private boolean updateTransDaily() {
//        boolean ret = false;
//        TrnDaily td = this.trnDaily;
//        td.setHospitalCode(this.lHospitalCode);
//        td.setInvoiceNo(this.lInvNo);
//        td.setInvoiceDate(this.lInvDate);
//        td.setLineNo(this.lLineNo);
//        td.setOrderItemCode(this.lOrderItemCode);
//        td.setDoctorCode(this.lDoctorCode);
//        td.setAmountBefDiscount(this.lRecAmt);
//        td.setAmountOfDiscount(this.lInvDiscountAmt);
//        td.setAmountAftDiscount(this.lInvAmt);
////        td.setPremiumRecAmt(this.lTotPremiumAmt);
////        td.setNorAllocateAmt(this.lNorAllocateAmt);
////        td.setNorAllocatePct(this.lNorAllocatePct);
////        td.setDrAmt(this.lDrAmt);
////        td.setDrPremium(this.lDrPremium);
////        td.setDrTax400(this.lDrTax400);
////        td.setDrTax401(this.lDrTax401);
////        td.setDrTax402(this.lDrTax402);
////        td.setDrTax406(this.lDrTax406);
////        td.setHpAmt(this.lHpAmt);
////        td.setHpPremium(this.lHpPremium);
////        td.setHpTax(this.lHpTax);
////        td.setTaxTypeCode(this.lTaxTypeCode);
////        td.setAdmissionType(this.lAdmissionTypeCode);
//        td.setPremiumChargePct(this.lPremiumChargePct);
////        td.setDoctorCategoryCode(this.lDoctorCategoryCode);
//        td.setExcludeTreatment(this.lExcludeTreatment);
////        td.setPremiumRecAmt(this.lPremiumRecAmt);
//        td.setComputeDailyDate(JDate.getDate());
//        td.setComputeDailyTime(JDate.getTime());
//        td.setComputeDailyUserID(Variables.getUserID());
//        
//        
////        td.setDoctorDepartmentCode(this.lDoctorDepartmentCode);
////        td.setLocationCode(this.lLocationCode);
////        td.setBatchNo("");
//        td.setUpdateDate(JDate.getDate());
//        td.setUpdateTime(JDate.getTime());
////        td.setUserId(Variables.getUserID());
////        td.setOldDoctorCode(this.lDoctorCode);
//        td.setInvIsVoid(this.lInvIsVoid);
////        td.setReceiptTypeCode(this.lReceiptTypeCode);
////        td.setReceiptModule("");
////        td.setReceiptType("");
//        td.setRecIsVoid(this.lRecIsVoid);
//        td.setActive("1");
////        td.setRecAmountBefDiscount(this.lRecAmt);
////        td.setRecAmountOfDiscount(0d);
////        td.setRecPremiumAmt(this.lTotPremiumAmt);
////        td.setReceiptModeCode(this.lReceiptModeCode);
//
////        td.setDoctorTreatmentCode(this.lDoctorTreatmentCode);
//
//        td.setTransactionDate(JDate.getDate());
//
//        // insert
//        ret = true;
//        ret = td.update();
//        td = null;
//        return ret;
//    }
    
//    private boolean insertSummaryDaily() {
//        boolean ret = false;
//        if (sd == null) { sd = new SummaryDaily(this.getDBConnection()); }
//        
//        sd.setHospitalCode(trnDaily.getHospitalCode());
//        sd.setInvoiceNo(trnDaily.getInvoiceNo());
//        sd.setInvoiceDate(trnDaily.getInvoiceDate());
//        sd.setTransactionDate(trnDaily.getTransactionDate());
//        sd.setHnNo(trnDaily.getHnNo());
//        sd.setPatientName(trnDaily.getPatientName());
//        sd.setEpisodeNo(trnDaily.getEpisodeNo());
//        sd.setPayorOfficeCode(trnDaily.getPayorOfficeCode());
//        sd.setPayorOfficeName(trnDaily.getPayorOfficeName());
//        sd.setTransactionModule(trnDaily.getTransactionModule());
//        sd.setTransactionType(trnDaily.getTransactionType());
//        sd.setPayorOfficeCategoryCode(trnDaily.getPayorOfficeCategoryCode());
//        sd.setPayorOfficeCategoryDescription(trnDaily.getPayorOfficeCategoryDescription());
//        sd.setIsWriteOff(trnDaily.getIsWriteOff());
//        sd.setLineNo(trnDaily.getLineNo());
//        sd.setAdmissionTypeCode(trnDaily.getAdmissionTypeCode());
//        sd.setNationalityCode(trnDaily.getNationalityCode());
//        sd.setNationalityDescription(trnDaily.getNationalityDescription());
//        sd.setPatientDepartmentCode(trnDaily.getPatientDepartmentCode());
//        sd.setPatientLocationCode(trnDaily.getPatientLocationCode());
//        sd.setReceiptDepartmentCode(trnDaily.getReceiptDepartmentCode());
//        sd.setReceiptLocationCode(trnDaily.getReceiptLocationCode());
//        sd.setDoctorDepartmentCode(trnDaily.getDoctorDepartmentCode());
//        sd.setOrderItemCode(trnDaily.getOrderItemCode());
//        sd.setOrderItemDescription(trnDaily.getOrderItemDescription());
//        sd.setDoctorCode(trnDaily.getDoctorCode());
//        sd.setVerifyDate(trnDaily.getVerifyDate());
//        sd.setVerifyTime(trnDaily.getVerifyTime());
//        sd.setDoctorExecuteCode(trnDaily.getDoctorExecuteCode());
//        sd.setExecuteDate(trnDaily.getExecuteDate());
//        sd.setExecuteTime(trnDaily.getExecuteTime());
//        sd.setDoctorResultCode(trnDaily.getDoctorResultCode());
//        sd.setOldDoctorCode(trnDaily.getDoctorCode());
//        sd.setReceiptTypeCode(trnDaily.getReceiptTypeCode());
//        sd.setAmountBefDiscount(trnDaily.getAmountBefDiscount());
//        sd.setAmountOfDiscount(trnDaily.getAmountOfDiscount());
//        sd.setAmountAftDiscount(trnDaily.getAmountAftDiscount());
//        sd.setAmountBefWriteOff(trnDaily.getAmountBefWriteOff());
//        sd.setInvIsVoid(trnDaily.getInvIsVoid());
//        sd.setRecIsVoid(trnDaily.getRecIsVoid());
//        sd.setUpdateDate(JDate.getDate());
//        sd.setUpdateTime(JDate.getTime());
//        sd.setUserId(Variables.getUserID());
//        sd.setInvoiceType(trnDaily.getInvoiceType());
//        sd.setTotalBillAmount(trnDaily.getTotalBillAmount());
//        sd.setTotalDrRecAmount(trnDaily.getTotalDrRecAmount());
//        sd.setOldAmount(trnDaily.getAmountAftDiscount());
//        sd.setActive(trnDaily.getActive());
//        
//        sd.setTaxTypeCode(this.lTaxTypeCode);
//        sd.setPremiumRecAmt(this.lTotPremiumAmt);
//        sd.setNorAllocateAmt(this.lNorAllocateAmt);
//        sd.setNorAllocatePct(this.lNorAllocatePct);
//        sd.setDrAmt(this.lDrAmt);
//        sd.setDrPremium(this.lDrPremium);
//        sd.setDrTax400(this.lDrTax400);
//        sd.setDrTax401(this.lDrTax401);
//        sd.setDrTax402(this.lDrTax402);
//        sd.setDrTax406(this.lDrTax406);
//        
//        sd.setHpAmt(this.lHpAmt);
//        sd.setHpPremium(this.lHpPremium);
//        sd.setHpTax(this.lHpTax);
//        
//        sd.setPremiumChargePct(this.lPremiumChargePct);
//        sd.setDoctorCategoryCode(this.lDoctorCategoryCode);
//        sd.setExcludeTreatment(this.lExcludeTreatment);
//        sd.setPremiumRecAmt(this.lPremiumRecAmt);
//        sd.setComputeDailyDate(JDate.getDate());
//        sd.setComputeDailyTime(JDate.getTime());
//        sd.setComputeDailyUserID(Variables.getUserID());
//        
//        sd.setYyyy(trnDaily.getYyyy());
//        sd.setMm(trnDaily.getMm());
//        sd.setReceiptNo(trnDaily.getReceiptNo());
//        sd.setReceiptDate(trnDaily.getReceiptDate());
//        sd.setPayByCash(trnDaily.getPayByCash());
//        sd.setPayByAR(trnDaily.getPayByAR());
//        sd.setPayByDoctor(trnDaily.getPayByDoctor());
//        sd.setPayByPayor(trnDaily.getPayByPayor());
//        sd.setPayByCashAr(trnDaily.getPayByCashAr());
//        sd.setIsPaid(trnDaily.getIsPaid());
//        
//        // insert
////        ret = true;
//        ret = sd.insert();
////        sd = null;
//        return ret;
//    }
    
    // ----------------------------- Transaction Monthly ------------------------------ //
     // ----------------------------- Transaction Monthly ------------------------------ //
      // ----------------------------- Transaction Monthly ------------------------------ //
       // ----------------------------- Transaction Monthly ------------------------------ //
        // ----------------------------- Transaction Monthly ------------------------------ //
    public boolean computeTransMonthly() {
        boolean result = false;
        try {
        	//System.out.println("Call compute summary");
            // call for compute summary monthly
            SummaryMonthly sm = new SummaryMonthly(this.getDBConnection());
            result = this.computeTransMonthly(sm);
            if (!result) { sm = null; return false; }

            double totAmt=0;
            totAmt = sm.getDrSumAmt();

            double gaurunteeAmt = 0d;

            // insert into SUMMARY_MONTHLY table
            sm.setBatchNo("");
            sm.setHospitalCode(this.getHospitalCode());
            sm.setDoctorCode(this.getDoctorCode());
            sm.setMm(Batch.getMm());
            sm.setYyyy(Batch.getYyyy());
            sm.setTransactionDate(JDate.getDate());

    /*                sm.setSumAmt(rs.getDouble("sinv_amt"));
                sm.setSumDiscAmt(rs.getDouble("sinv_discount_amt"));
                sm.setSumPatientPaidAmt(rs.getDouble("sRec_Amt"));
                sm.setSumPremiumAmt(rs.getDouble("sRec_Premium_Amt"));
                sm.setDrSumAmt(rs.getDouble("sDr_Amt"));
                sm.setDrTax402(rs.getDouble("sDr_Amt_402"));
                sm.setDrTax406(rs.getDouble("sDr_Amt_406"));
                sm.setDrPremiumAmt(rs.getDouble("sDr_Premium"));
                sm.setHpSumAmt(rs.getDouble("sHp_Amt"));
                sm.setHpPremiumAmt(rs.getDouble("sHp_Premium"));
                sm.setHpTax(rs.getDouble("sHp_Tax")); */

            // เพิ่ม payroll paid and payroll tax 40(2)
            sm.setGuarunteeAmt(gaurunteeAmt);
            sm.setGuarunteeTax(gaurunteeAmt);

            // get expense
//            TrnExpenseHeader exp = new TrnExpenseHeader(Batch.getYyyy(),Batch.getMm(), Batch.getHospitalCode(), this.getDoctorCode(), this.getDBConnection());
//            sm.setDrCreditAmt(exp.getCreditAmount());
//            sm.setDrDebitAmt(exp.getDebitAmount());
            sm.setDrCreditAmt(0d);
            sm.setDrDebitAmt(0d);
            sm.setDrDiscAmt(0.0);
            sm.setHpDiscAmt(0.0);
            sm.setStatusModify("");
            sm.setUserID(Variables.getUserID());
            sm.setUpdateDate(JDate.getDate());
            sm.setUpdateTime(JDate.getTime());
            sm.setOldDoctorCode(this.getDoctorCode());

            double drNetPaidAmt = sm.getDrSumAmt() - sm.getDrDiscAmt() - sm.getDrPremiumAmt() 
                                - sm.getDrCreditAmt() + sm.getDrDebitAmt();// + sm.getGuarunteeAmt();

            sm.setDrNetPaidAmt(drNetPaidAmt);
            //sm.setRemark(Utils.toThaiMoney(sm.getDrNetPaidAmt()));
            sm.setRemark("");
            sm.setRefPaidNo(this.getDoctor().getBankAccountNo());
            sm.setPaymentModeCode(this.getDoctor().getPaymentModeCode());
            
            // Payment
            if (sm.getPaymentModeCode().equals(PaymentMode.PAYMENT_CHEQUE)) {  sm.setPayDate("");  } 
            else { sm.setPayDate(Batch.getPaymentDate()); }
            sm.setPaymentTermDate(Batch.getPaymentDate());
            sm.setIsHold(this.getDoctor().getIsHold());
             
            // insert data to SUMMARY_MONTHLY
            if (drNetPaidAmt != 0 ) {
                if (!sm.insert()) { 
                    this.setMessage("Can't insert data into table !!!!");
                    sm = null;
                    return false;
                } 
            }
                
            sm = null;

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("computeTransMonthly" + e.getMessage());
            this.setMessage("Monthly calculate fail.");
////            DialogBox.ShowError("Error in CareProvider.computeMonthly() \n" + e.getMessage());
            TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), TRN_Error.PROCESS_DAILY, "Monthly calculate fail.", e.getMessage(), "");
            return false;
        } finally {
               //Clean up resources, close the connection.
                try {
                    if(rs != null) {
                        rs.close();
                        rs = null;
                    }
                } catch (Exception ignored) { ignored.printStackTrace();   }
            }

        return true;
    }
    
    // code เหมือน DrFullTime แต่ไม่มี EXCLUDE_TREATMENT
    private boolean computeTransMonthly(SummaryMonthly sm) {
        String sql = SummaryMonthly.getSQLSum_DAILY(this.getHospitalCode());
        System.out.println("Q : "+sql);
        if (smVar == null) {  
            smVar = new SummaryMonthlyVar(this.conn); 
            smVar.getDataByQuery(sql);
        }
        
        if (smVar.IsFoundAndSetData(this.getDoctorCode())) {
            sm.setSumAmt(smVar.getSumAmountAftDiscount());
            sm.setSumDiscAmt(smVar.getSumAmountOfDiscount());
            sm.setSumPatientPaidAmt(smVar.getSumRecAmountAftDiscount());
            sm.setSumPremiumAmt(smVar.getSumRecPremiumAmt());
            sm.setDrSumAmt(smVar.getSumDrAmt());
            sm.setDrTax400(smVar.getSumDrAmt_400());
            sm.setDrTax401(smVar.getSumDrAmt_401());
            sm.setDrTax402(smVar.getSumDrAmt_402());
            sm.setDrTax406(smVar.getSumDrAmt_406());
            sm.setDrPremiumAmt(smVar.getSumDrPremium());
            sm.setHpSumAmt(smVar.getSumHpAmt());
            sm.setHpPremiumAmt(smVar.getSumHpPremium());
            sm.setHpTax(smVar.getSumHpTax());
            sm.setSumPremiumRecAmt(smVar.getSumPremiumRecAmt());
            sm.setDrDiscAmt(0.0);
            sm.setHpDiscAmt(0.0); 
            sm.setSumPayByCash(smVar.getSumPayByCash());
            sm.setSumPayByAR(smVar.getSumPayByAR());
            
            sm.setCashAmt(smVar.getCashAmt());
            sm.setCash400(smVar.getCash400());
            sm.setCash401(smVar.getCash401());
            sm.setCash402(smVar.getCash402());
            sm.setCash406(smVar.getCash406());
            sm.setArAmt(smVar.getArAmt());
            sm.setAr400(smVar.getAr400());
            sm.setAr401(smVar.getAr401());
            sm.setAr402(smVar.getAr402());
            sm.setAr406(smVar.getAr406());
        }
        return true;
    }
    // ----------------------------- End of Transaction Monthly ------------------------------ //
     // ----------------------------- End of Transaction Monthly ------------------------------ //
      // ----------------------------- End of Transaction Monthly ------------------------------ //
       // ----------------------------- End of Transaction Monthly ------------------------------ //
        
                
}